﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using fyiReporting.RdlViewer;
using System.Drawing.Printing;

namespace WinCrystalReports
{
    public partial class Form1 : Form
    {
        public const String REPORT_SOURCE = @"..\..\InformeFlyReporting\informeContactes.rdl";
        public const String REPORT_SOURCE_GRAFIC = @"..\..\InformeFlyReporting\informeGrafic.rdl";
        public const String REPORT_EXPORT_HTML = REPORT_SOURCE + ".html";

        RdlViewer rdlView;
        RdlViewer rdlViewGrafics;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dtoContactosEmpresa datos = ContactoDAL.GetContactosEmpresa();
            
        
            //Carrega informe del gràfic.
            rdlViewGrafics = new RdlViewer();
            rdlViewGrafics.ReportName = "grafic";
            rdlViewGrafics.Dock = DockStyle.Fill;

            rdlViewGrafics.SourceFile = REPORT_SOURCE_GRAFIC;
            rdlViewGrafics.ShowParameterPanel = true;

            pnl.Controls.Add(rdlViewGrafics);
            rdlViewGrafics.Visible = true;
            rdlViewGrafics.Refresh();

            //Carrega l'informe convencional.
            rdlView = new RdlViewer();
            rdlView.ReportName = "prova";
            rdlView.Dock = DockStyle.Fill;

            rdlView.SourceFile = REPORT_SOURCE;
            rdlView.ShowParameterPanel = true;

            pnl.Controls.Add(rdlView);
            rdlView.Visible = true;
            rdlView.Refresh();
        }

        private void btnExportarAHTML_Click(object sender, EventArgs e)
        {
            RdlViewer rdl = new RdlViewer();
            rdl.SourceFile = REPORT_SOURCE;
            rdl.SaveAs(REPORT_EXPORT_HTML, "html");
        }

        private void btnImprimirInforme_Click(object sender, EventArgs e)
        {
            PrintDocument pd = new PrintDocument();
            RdlViewer rdlViewer = new RdlViewer();
            rdlViewer.SourceFile = REPORT_SOURCE;
            pd.DocumentName = rdlViewer.SourceFile == null ? "untitled" : rdlViewer.SourceFile;
            pd.PrinterSettings.FromPage = 1;
            pd.PrinterSettings.ToPage = rdlViewer.PageCount;
            pd.PrinterSettings.MaximumPage = rdlViewer.PageCount;
            pd.PrinterSettings.MinimumPage = 1;
            if (rdlViewer.PageWidth > rdlViewer.PageHeight)
                pd.DefaultPageSettings.Landscape = true;
            else
                pd.DefaultPageSettings.Landscape = false;

            PrintDialog dlg = new PrintDialog();
            dlg.Document = pd;
            dlg.AllowSelection = true;
            dlg.AllowSomePages = true;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if (pd.PrinterSettings.PrintRange == PrintRange.Selection)
                    {
                        pd.PrinterSettings.FromPage = rdlViewer.PageCurrent;
                    }
                    rdlViewer.Print(pd);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Print error: " + ex.Message);
                }
            }
        }

        private void btnVisualitzarInforme_Click(object sender, EventArgs e)
        {
            rdlView.Visible = true;
            rdlViewGrafics.Visible = false;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            rdlViewGrafics.Visible = true;
            rdlView.Visible = false;
        }

    }
}
